package cst.a112gxl.TeacherPlatform112gxl.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import cst.a112gxl.TeacherPlatform112gxl.R;
import cst.a112gxl.TeacherPlatform112gxl.listener.AmicoolListener;
import cst.a112gxl.TeacherPlatform112gxl.model.Resmodel;

/**
 * Created by Administrator on 2017/6/3/003.
 */

public class ViewArticleActivity extends AppCompatActivity implements View.OnClickListener {
    private int resid;
    private Boolean isCollect,isFoucs;
    private WebView webView;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    private String mod,sessionid;
    private FloatingActionButton bcollect,bfocus;
    private Resmodel resmodel;
    private AmicoolListener<String> clistener,aclistener,unclistener,flistener,aflistener,unflistener;
    private SharedPreferences sp;
    private Context context;
    private Integer number;
    private VideoView videoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_article);
        context = getBaseContext();
        sp = context.getSharedPreferences("teach", Context.MODE_PRIVATE);
        intit();
        sessionid = sp.getString("sessionid",null);
        resid  = getIntent().getIntExtra("resid",1);
        mod = getIntent().getStringExtra("mod");
        resmodel.iscollect(mod,resid,sessionid,clistener);
        resmodel.isfocus("userfocus",resid,sessionid,flistener);
        webView.loadUrl(BASEURL+mod+".php/show/index/id/"+resid);

}

    void intit(){
        bcollect = (FloatingActionButton)findViewById(R.id.shoucang);
        bfocus = (FloatingActionButton)findViewById(R.id.guanzhu);
        webView = (WebView)findViewById(R.id.webview);
        videoView = (VideoView)findViewById(R.id.videoView);
        bcollect.setOnClickListener(this);
        bfocus.setOnClickListener(this);
        resmodel = new Resmodel("string");
        clistener = new AmicoolListener<String>() {
            @Override
            public void onResponse(String l) {
                if (l.equals("0")){
                    bcollect.setImageResource(R.drawable.collect_press);
                    isCollect = true;
                }else{
                    isCollect = false;
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ViewArticleActivity.this, "收藏状态获取失败", Toast.LENGTH_SHORT).show();
            }
        };

        aclistener = new AmicoolListener<String>() {
            @Override
            public void onResponse(String l) {

                if (!l.equals("0")){
                    bcollect.setImageResource(R.drawable.collect_press);
                    Toast.makeText(ViewArticleActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    isCollect = true;
                }else
                    Toast.makeText(ViewArticleActivity.this, "收藏失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ViewArticleActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        unclistener =  new AmicoolListener<String>() {
            @Override
            public void onResponse(String l) {
                if (l.equals("1")){
                    bcollect.setImageResource(R.drawable.collect_normal);
                    Toast.makeText(ViewArticleActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();

                    isCollect = false;
                }else
                    Toast.makeText(ViewArticleActivity.this, "取消收藏失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ViewArticleActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
            }
        };

        flistener = new AmicoolListener<String>() {
            @Override
            public void onResponse(String l) {
                if (l.equals("0")){
                    bfocus.setImageResource(R.drawable.focus_press);
                    isFoucs = true;
                }else{
                    isFoucs = false;
                }

            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ViewArticleActivity.this, "关注状态获取失败", Toast.LENGTH_SHORT).show();
            }
        };

        aflistener = new AmicoolListener<String>() {
            @Override
            public void onResponse(String l) {

                if (!l.equals("0")){
                    bfocus.setImageResource(R.drawable.focus_press);
                    Toast.makeText(ViewArticleActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
                    isFoucs = true;
                }else
                    Toast.makeText(ViewArticleActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
                System.out.println("123123");
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ViewArticleActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        unflistener =  new AmicoolListener<String>() {
            @Override
            public void onResponse(String l) {
                if (l.equals("1")){
                    bfocus.setImageResource(R.drawable.focus_normal);
                    Toast.makeText(ViewArticleActivity.this, "取消关注", Toast.LENGTH_SHORT).show();

                    isFoucs = false;
                }else
                    Toast.makeText(ViewArticleActivity.this, "取消关注失败", Toast.LENGTH_SHORT).show();
                System.out.println("666666");
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ViewArticleActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
            }
        };


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.shoucang:
                if(isCollect==false){
                    resmodel.collect(mod,resid,sessionid,aclistener);
                }else {
                    resmodel.uncollect(mod,resid,sessionid,unclistener);
                }
                break;
            case R.id.guanzhu:
                if(isFoucs==false){
                    resmodel.focus("userfocus",resid,sessionid,aflistener);
                }else {
                    resmodel.unfoucs("userfocus",resid,sessionid,unflistener);
                }
                break;
        }
    }
}
